<?php
/**
 * Created by PhpStorm.
 * User: cxj
 * Date: 16/12/26
 * Time: 下午2:51
 */
namespace app\controllers;

use yii\web\Controller;
use app\models\Activity;

class ActivityController extends  Controller{
    public function init(){
        $this->enableCsrfValidation = false;
    }
    public function actionIndex(){
        $view = \Yii::$app->view;
        $view->params['menuItem']=2;
        $request = \Yii::$app->request;
        $status = $request->get('status');
        $model = new Activity();
        if($status == 0){
            $activities = $model->getActivities();
            $total = count($activities);
            $pageSize = ceil($total/2);
            $activityContent = array_slice($activities,0,2);
        }else if ($status == 3){
            $activities = $model->getActivities();
            $now =gmdate('Y-m-d H:i', time() + 3600 * 8);
            for($i=0;$i<count($activities);$i++){
                if($activities[$i]['over_time']<$now){
                    //跟新状态为3
                    \Yii::$app->db->createCommand()
                        ->update('activity', ['status' => 3], ['id'=>$activities[$i]['id']])->execute();
                }
            }
            $done = Activity::find()->where(['status'=>3])->asArray()->all();
            $total = count($done);
            $pageSize = ceil($total/2);
            $activityContent = array_slice($done,0,2);

        } else{
            $activities = $model->getByStatus($status);
            $total = count($activities);
            $pageSize = ceil($total/2);
            $activityContent = array_slice($activities,0,2);

        }

        return $this->render('index',[
            'status' => $status,
            'total' => $total,
            'pageSize' => $pageSize,
            'activityContent' => $activityContent
        ]);
    }

    public function actionUploader()
    {
        if ((($_FILES["img"]["type"] == "image/gif")
                || ($_FILES["img"]["type"] == "image/jpeg")
                || ($_FILES["img"]["type"] == "image/png"))
            && ($_FILES["img"]["size"] < 5120000))
        {
            if ($_FILES["img"]["error"] > 0)
            {
                echo '';
            }
            else
            {

                if (file_exists("upload/" . $_FILES["img"]["name"]))
                {
                    $url='http://'.$_SERVER['SERVER_NAME'].'/guochuang/basic/web';
                    echo $url. "/upload/" . $_FILES["img1"]["name"];
                }
                else
                {

                    $url='http://'.$_SERVER['SERVER_NAME'].'/guochuang/basic/web';
                    move_uploaded_file($_FILES["img"]["tmp_name"],
                        "upload/" . $_FILES["img"]["name"]);
                    echo $url. "/upload/" . $_FILES["img"]["name"];
                }
            }
        }
        else
        {
            echo "";
        }

    }

    public function actionGetActivity(){
        $request=\Yii::$app->request;
        $pageNum=$request->post('pageNum');
        $status = $request->post('status');
        $model=new Activity();
        if($status == 0){
            $activities = $model->getActivities();
        }else{
            $activities = $model->getByStatus($status);
        }
        $res=$model->getActivityByPageNum($activities,$pageNum);
        echo $result = json_encode($res);

    }
    //删除活动
    public function actionDelete(){
        $id = \Yii::$app->request->post('id');
        \Yii::$app->db->createCommand()
            ->update('activity', ['del_flag' => 0], ['id'=>$id])->execute();
    }
    //发布页面
    public function actionPublish(){
        $view = \Yii::$app->view;
        $view->params['menuItem']=3;

        return $this->render('publish');
    }

    //编辑活动
    public function actionEdit($id){
        $model = new Activity();
        $data = $model->getById($id);//这个活动的数据 数组
        $view = \Yii::$app->view;
        $view->params['menuItem']= 5;
        var_dump($data);
        return $this->render('edit',['data'=>$data]);
    }
    //发布活动
    public function actionIssue(){

        $request = \Yii::$app->request;
        $poster = $request->post('poster');
        $_title = $request->post('title');
        $city = $request->post('city');
        $address = $request->post('address');
        $start_time = $request->post('start_time');
        $over_time = $request->post('over_time');
        $deadline = $request->post('deadline');
        $sponsor = $request->post('sponsor');
        $detail = $request->post('detail');
        $category = $request->post('category');
        $is_show = $request->post('is_show');
        $is_stick = $request->post('is_stick');

        $ticket = $request->post('ticket');
        $status= $request->post('status');
        $price= $request->post('price');
        $num= $request->post('num');

        $model = new Activity();
        $model->poster = $poster;
        $model->_title = $_title;
        $model->city = $city;
        $model->address = $address;
        $model->start_time = $start_time;
        $model->over_time = $over_time;
        $model->deadline = $deadline;
        $model->sponsor = $sponsor;
        $model->detail = $detail;
        $model->category = $category;
        $model->is_show = $is_show;
        $model->is_stick = $is_stick;
        $model->ticket = $ticket;
        $model->status = $status;
        $model->price = $price;
        $model->num = $num;

        if($model->save()){
            echo json_encode('1');//成功
        }else{
            echo json_encode('0');//失败
        }

    }

    public function actionChangeStatus(){
        $id = \Yii::$app->request->get('id');
        \Yii::$app->db->createCommand()
            ->update('activity', ['status' => 2], ['id'=>$id])->execute();
    }

    public function actionChangeTwo(){
        $id = \Yii::$app->request->get('id');
        \Yii::$app->db->createCommand()
            ->update('activity', ['status' => 1], ['id'=>$id])->execute();
    }

    public function actionSign(){
        $view = \Yii::$app->view;
        $view->params['menuItem']=6;

        return $this->render('sign');
    }
    public function actionUpload(){
        var_dump($_FILES);

        if ($_FILES["upload-file"]["size"] < 5120000)
        {
            if ($_FILES["upload-file"]["error"] > 0)
            {
                echo $_FILES["upload-file"]["error"] . "<br />";
            }
            else
            {
                if (file_exists("../upload/" . $_FILES["file"]["name"]))
                {
                    echo $_FILES["upload-file"]["name"] . " 上传成功 ";
                }
                else
                {
                    move_uploaded_file($_FILES["upload-file"]["tmp_name"],
                        "upload/" . $_FILES["file"]["name"]);
                    echo "上传成功";
                }
            }
        }
        else
        {
            echo "上传失败";
        }
    }
}